﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CSEPT.Controllers
{
    public class UserController : Controller
    {
        [LoggedIn]
        public ActionResult Index(){
            Models.UserViewModel UVM = new Models.UserViewModel();
            UVM.RegisteredEvents = UVM.dal.GetEvents(UVM.CurrUser);
            UVM.TaggedPhotos = UVM.dal.GetTaggedPhotos(UVM.CurrUser);
            return View("Index", UVM);
        }
        public ActionResult Register()
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            return View(UVM);
        }
        public ActionResult Create(User Usr, string Password)
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            try
            {
                if (String.IsNullOrEmpty(Usr.UserName))
                {
                    UVM.PageMessage = "User Name cannot be blank";
                }
                if (String.IsNullOrEmpty(Usr.Email))
                {
                    UVM.PageMessage += "<br/>Email cannot be blank";
                }
                if (String.IsNullOrEmpty(Password))
                {
                    UVM.PageMessage += "<br/>Password cannot be blank";
                }

                if (!String.IsNullOrEmpty(UVM.PageMessage))
                {
                    throw new Exception(UVM.PageMessage);
                }

                UVM.dal.CreateUser(Usr, UVM.dal.EncryptPassword(Password));
                Login(Usr.UserName, UVM.dal.EncryptPassword(Password));

            }
            catch (Exception e)
            {
                UVM.PageMessage = e.Message;
                UVM.PageMessageType = "danger";
                return View("Register", UVM);
            }
            return RedirectToAction("IndexInfo", "Index", new {@Message = "User account created", @Type = "success"});
        }
        [LoggedIn]
        public ActionResult Info()
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            UVM.usr = UVM.CurrUser;
            return View(UVM);
        }
        [LoggedIn]
        public ActionResult Update(User Usr)
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            UVM.dal.UpdateUser(Usr);
            return RedirectToAction("Index", "User");
        }
        [LoggedIn]
        public ActionResult Password()
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            UVM.usr = UVM.CurrUser;
            return View(UVM);
        }
        [LoggedIn]
        public ActionResult UpdatePassword(User Usr,string OldPassword, string NewPassword)
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            try
            {
                if (!UVM.isLoggedIn)
                {
                    throw new Exception("User not logged in");
                }
                UVM.dal.AttemptLogin(Usr, UVM.dal.EncryptPassword(OldPassword));
                UVM.dal.UpdateUserPassword(Usr, UVM.dal.EncryptPassword(NewPassword));
            }
            catch (Exception e)
            {
                Log.Info(UVM.CurrUser.UserName + "User Failed to update password", "User.UpdatePassword");
                UVM.PageMessage = e.Message;
                UVM.PageMessageType = "warning";
                return View("Password", UVM);
            }
            return RedirectToAction("IndexInfo", "Index", new {@Message = "Password succesfully updated", @Type = "success"});
        }
                    
        public ActionResult Login(string UserName, string Password)
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            try
            {
                UVM.CurrUser = UVM.dal.AttemptLogin(UserName, UVM.dal.EncryptPassword(Password));
                Session["LoginID"] = UVM.CurrUser.ID;
                UVM.isLoggedIn = true;
            }
            catch(Exception){
                Log.Info(UserName + "Failed Login", "User.Login");
                UVM.PageMessage = "Invalid User Name or Password";
                return RedirectToAction("IndexInfo", "Index", new {@Message = UVM.PageMessage, @Type = "danger"});
            }
            return RedirectToAction("Index", "Index");
        }
        [LoggedIn]
        public ActionResult Logout()
        {
            Session["LoginID"] = null;
            return RedirectToAction("Index", "Index");
        }
        [LoggedIn]
        public ActionResult Remove()
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            return View(UVM);
        }
        [LoggedIn]
        public ActionResult Delete(int ID)
        {
            Models.UserViewModel UVM = new Models.UserViewModel();
            UVM.dal.DeleteUser(new User(ID));
            return Logout();
        }
    }
}
